package com.example.test.util;

import fr.opensagres.poi.xwpf.converter.xhtml.Base64EmbedImgManager;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;
import fr.opensagres.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hwpf.HWPFDocumentCore;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.converter.WordToHtmlUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

/**
 * word转html
 */
public class WordToHtml{
	
	/**
	 * doc转html
	 * @Author: 林文峰 
	 * @Date 2020/6/24 0024 16:50
	 */
	public static void docToHtml(MultipartFile file,String HtmlFilePath,String HtmlFileName) throws IOException, ParserConfigurationException, TransformerException {
		File f = MultipartFileToFile.multipartFileToFile(file);
		HWPFDocumentCore wordDocument = WordToHtmlUtils.loadDoc(new FileInputStream(f));
		WordToHtmlConverter wordToHtmlConverter = new ImageConverter(
				DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
		);
		wordToHtmlConverter.processDocument(wordDocument);
		Document htmlDocument = wordToHtmlConverter.getDocument();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		DOMSource domSource = new DOMSource(htmlDocument);
		StreamResult streamResult = new StreamResult(out);
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer serializer = transformerFactory.newTransformer();
		serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
		serializer.setOutputProperty(OutputKeys.INDENT, "yes");
		serializer.setOutputProperty(OutputKeys.METHOD, "html");
		serializer.transform(domSource, streamResult);
		out.close();
		String result = new String(out.toByteArray());
		FileUtils.writeStringToFile(new File(HtmlFilePath, HtmlFileName+".html"), result, "utf-8");
	}

	
	/**
	 * docx转换html
	 * @Author: 林文峰 
	 * @Date 2020/6/24 0024 16:50
	 */
	public static String docxToHtml(MultipartFile file,String HtmlFilePath,String HtmlFileName) throws IOException {
		File f = MultipartFileToFile.multipartFileToFile(file);
		XWPFDocument docxDocument = new XWPFDocument(new FileInputStream(f));
		XHTMLOptions options = XHTMLOptions.create();
		//图片转base64
		options.setImageManager(new Base64EmbedImgManager());
		// 转换htm11
		ByteArrayOutputStream htmlStream = new ByteArrayOutputStream();
		XHTMLConverter.getInstance().convert(docxDocument, htmlStream, options);
		String htmlStr = htmlStream.toString();
		htmlStr = htmlStr.replaceAll("data:;base64","data:image/png;base64");
		htmlStream.close();
		docxDocument.close();
		FileUtils.writeStringToFile(new File(HtmlFilePath, HtmlFileName+".html"),htmlStr, "utf-8");
		MultipartFileToFile.deleteTempFile(f);
		return htmlStr;
	}

	
}